import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:yinshui_caiying/services/common_services.dart';
import 'package:yinshui_caiying/services/location_services.dart';
import 'package:yinshui_caiying/services/system_sercvices.dart';
import 'package:yinshui_caiying/services/user_services.dart';
import 'package:yinshui_caiying/utils/colors.dart';

import 'routers/route.dart';

Future<void> main() async {
  //顶部状态栏透明
  // SystemChrome.setSystemUIOverlayStyle(
  //     SystemUiOverlayStyle(statusBarColor: Color.fromRGBO(11, 193, 173, 1)));
  await initServices();

  /// 等待服务初始化.
  runApp(MyApp());

  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: MyColors.main);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

/// 在你运行Flutter应用之前，让你的服务初始化是一个明智之举。
////因为你可以控制执行流程（也许你需要加载一些主题配置，apiKey，由用户自定义的语言等，所以在运行ApiService之前加载SettingService。
///所以GetMaterialApp()不需要重建，可以直接取值。
initServices() async {
  WidgetsFlutterBinding.ensureInitialized();

  ///加载用户信息服务
  await Get.putAsync(() => UserService().init());

  ///加载地图服务
  await Get.putAsync(() => LocationService().init());

  ///加载系统服务
  await Get.putAsync(() => SystemService().init());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      ///适配机型屏幕（设计图尺寸）
      designSize: Size(1080, 1920),

      ///app字体跟随系统字体大小进行缩放
      allowFontScaling: false,
      builder: () => GetMaterialApp(
        ///关闭右上角测试条
        debugShowCheckedModeBanner: false,

        title: '城乡水务-彩影',

        ///绑定路由表
        getPages: routes,

        ///设置全局主题
        theme: ThemeData(primaryColor: Colors.white),

        ///国际化设置（解决长按TextFiled出现英文（复制、粘贴）的问题）
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('zh', 'CN'),
          const Locale('en', 'US'),
        ],

        ///全局配置空白处点击取消输入框焦点，全局配置FlutterSmartDialog弹窗
        builder: (BuildContext context, child) {
          return FlutterSmartDialog(
              child: Scaffold(
            body: GestureDetector(
              onTap: () {
                FocusScopeNode currentFocus = FocusScope.of(context);
                if (!currentFocus.hasPrimaryFocus &&
                    currentFocus.focusedChild != null) {
                  FocusManager.instance.primaryFocus!.unfocus();
                }
                // SystemChannels.textInput.invokeMethod('TextInput.hide');
              },
              child: child,
            ),
          ));
        },

        ///设置初始化路由页面
        initialRoute: "/login",
      ),
    );
  }
}
